/**
 * ©2013-2015 Alan L. Rights Reserved.
 */
package org.freedom.railhunter.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.JsonNode;
import org.freedom.railhunter.util.HttpConnectionManager;
import org.freedom.railhunter.config.PathConfig;
import org.freedom.railhunter.exception.LoginFailedException;
import org.freedom.railhunter.model.AcceptDataType;
import static org.freedom.railhunter.service.BaseService.DATA;
import static org.freedom.railhunter.service.BaseService.JSON_ATTR;
import static org.freedom.railhunter.service.BaseService.object_mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Alan
 */
public class LoginService extends BaseService {
    
    private static final Logger logger = LoggerFactory.getLogger(LoginService.class);
    
     public static final String LOGIN_USERNAME = "loginUserDTO.user_name";
    
    public static final String LOGIN_PASSWORD = "userDTO.password";
    
    public static final String LOGIN_RANDCODE = "randCode";
    
    private static LoginService  loginService;
    
    private LoginService() {
    }
    
    public static LoginService getInstance() {
        if (loginService == null) {
            loginService = new LoginService();
        }
        return loginService;
    }
    
    /**
     * 登录
     * 
     * @param userName
     * @param password
     * @param randCode
     * @param cookieData
     * @return 
     */
    public boolean login(String userName, String password, String randCode, Map<String, String> cookieData) 
            throws LoginFailedException {
        try {
            List<NameValuePair> loginSuggestParameters = new ArrayList<>();
            loginSuggestParameters.add(new BasicNameValuePair(LOGIN_USERNAME, userName));
            loginSuggestParameters.add(new BasicNameValuePair(LOGIN_PASSWORD, password));
            loginSuggestParameters.add(new BasicNameValuePair(LOGIN_RANDCODE, randCode));

            String result = HttpConnectionManager.postHttpRequestAsString(PathConfig.LOGIN_AYSN_SUGGEST_PATH,
                    PathConfig.LOGIN_INIT_PATH, AcceptDataType.All, loginSuggestParameters, cookieData);
            JsonNode root = object_mapper.readTree(json_factory.createJsonParser(result));
            JsonNode data = root.get(DATA);
            if (data != null) {
                JsonNode loginCheck = data.get("loginCheck") ;
                if (loginCheck != null && "Y".equals(loginCheck.getTextValue())) {
                    List<NameValuePair> userLoginParameters = new ArrayList<>();
                        userLoginParameters.add(new BasicNameValuePair(JSON_ATTR, ""));
                        HttpConnectionManager.postHttpRequest(PathConfig.USER_LOGIN_PATH,
                                PathConfig.LOGIN_INIT_PATH, AcceptDataType.HTML, userLoginParameters, null, cookieData);
                        return true;
                }
            }
            JsonNode messageNode = root.get(MESSAGE);
            if (messageNode != null) {
                throw new LoginFailedException(messageNode.toString());
            }
        } catch (IOException ex) {
            throw new LoginFailedException("登录异常", ex);
        }
        return false;
    }
    
    /**
     * 登出
     * 
     * @param cookieData 
     */
    public void logout(Map<String, String> cookieData) {
        try {
            HttpConnectionManager.getHttpRequest(PathConfig.LOGIN_OUT_PATH, PathConfig.LEFT_TICKET_INIT_PATH, 
                    null, AcceptDataType.HTML, cookieData);
            cookieData.clear();
        } catch (IOException ex) {
            logger.error("登出异常", ex);
        }
    }
}
